package cn.xiaolang.thread.wait;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Executors线程池方式处理线程等待其他线程
 *
 * @author fuhw
 * @date 2017年8月17日 上午10:13:32
 */
public class ThreadWaitByExecutors {

    /**
     *
     * 执行结果： start create some thread: pool-1-thread-1executing.....
     * pool-1-thread-2executing..... pool-1-thread-3executing.....
     * pool-1-thread-4executing..... pool-1-thread-5executing.....
     * pool-1-thread-1executing..... pool-1-thread-2executing.....
     * pool-1-thread-3executing..... pool-1-thread-4executing.....
     * pool-1-thread-5executing..... all thread execute over!
     */
    public static void main(String[] args) {

        System.err.println("start create some thread:");
        ExecutorService executors = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            executors.execute(() -> {
                // do-something
                try {
                    Thread.sleep(1);
                    System.err.println(Thread.currentThread().getName() + "executing.....");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        executors.shutdown();
        while (true) {
            if (executors.isTerminated()) {
                System.err.println("all thread execute over!");
                break;
            }
        }
    }

}
